﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;//引入HalconDotNet命名空间

namespace Camera
{
    /// <summary>
    /// 相机工作状态
    /// </summary>
    public enum CamerWork
    {
        None,//等待
        采集一张,//采集一张
        连续采集,//采集多张
    }
    
    public abstract class AbstractCamera
    {
        /// <summary>
        /// 相机名称
        /// </summary>
        public string CameraName { get; set; }
        /// <summary>
        /// 品牌名称
        /// </summary>
        public string BrandName { get; set; }
        /// <summary>
        /// 读取的文件路径
        /// </summary>
        public string ImageFolderPath { get; set; }
        /// <summary>
        /// 读取到的图片集合
        /// </summary>
        public string[] ImageURL { get; set; }
        /// <summary>
        /// 读取到的图片索引
        /// </summary>
        public int Index { get; set; }
        /// <summary>
        /// 相机的工作状态
        /// </summary>
        public CamerWork CameraWorkState { get; set; } = CamerWork.None;//等待
        /// <summary>
        /// 相机读取图片后的延迟时间
        /// </summary>
        public abstract int DelayTime { get; set; }
        /// <summary>
        /// 读取到的图像
        /// </summary>
        public HObject Image;
        /// <summary>
        /// Halcon控件的句柄
        /// </summary>
        public HTuple HalconWindow;

        /// <summary>
        /// 绑定的控件
        /// </summary>
        public PictureBox pictureBox;

        /// <summary>
        /// 打开相机
        /// </summary>
        public virtual void OpenCamera() 
        {
            //读取指定路径下的图片
            ImageURL = System.IO.Directory.GetFiles(ImageFolderPath,"*.png");
            //设置相机状态
            CameraWorkState = CamerWork.None;
            //判断图片路径不为空
            if (!string.IsNullOrEmpty(ImageURL[Index]))
            {
                //读取指定路径下的图片
                HOperatorSet.ReadImage(out Image, ImageURL[Index]);
                //绑定窗体控件
                HOperatorSet.OpenWindow(0, 0, this.pictureBox.Width, this.pictureBox.Height, this.pictureBox.Handle, "visible", "", out HalconWindow);
                //获取图像大小
                HOperatorSet.GetImageSize(Image, out HTuple width, out HTuple height);
                //适应图像的宽与高
                HOperatorSet.SetPart(HalconWindow, 0, 0, height, width);
            }
        }
        /// <summary>
        /// 关闭相机
        /// </summary>
        public virtual void CloseCamera()
        {
            //设置相机状态
            CameraWorkState = CamerWork.None;
        }
        /// <summary>
        /// 采集一张
        /// </summary>
        public virtual void CollectionOne()
        {
            //设置相机状态
            CameraWorkState = CamerWork.采集一张;
        }
        /// <summary>
        /// 连续采集
        /// </summary>
        public virtual void ContiuouseAcquiation()
        {
            //设置相机状态
            CameraWorkState = CamerWork.连续采集;
        }
    }
}
